package top.hkyzf.study.leetcode.array;

import org.junit.Test;

/**
 * description: 
 * @author zhufeng
 * @date 2024/1/23 0023 9:15
 * @version V1.0
 */
public class 最长交替子数组_2765 {
    public int alternatingSubarray(int[] nums) {
        int rtn = -1;
        int temp = -1;
        int preDir = 0;
        int first = nums[0];
        int[] direct = {-1, 1};
        for (int i = 1; i < nums.length; i++) {
            if (first + direct[0] == nums[i] && temp != -1) {
                if (preDir == direct[0]) {
                    rtn = Math.max(rtn, temp);
                    temp = 2;
                } else {
                    temp++;
                }
                preDir = direct[0];
                first = nums[i];
            } else if (first + direct[1] == nums[i]) {
                if (preDir == direct[1]) {
                    rtn = Math.max(rtn, temp);
                    temp = 2;
                } else {
                    if (temp != -1) {
                        temp++;
                    } else {
                        temp = 2;
                    }
                }
                preDir = direct[1];
                first = nums[i];
            } else {
                rtn = Math.max(rtn, temp);
                temp = -1;
                preDir = 0;
                first = nums[i];
            }
        }
        rtn = Math.max(rtn, temp);
        return rtn;
    }
    
    @Test
    public void testSubarray () {
        int[] nums = {4,5,6};
        int i = alternatingSubarray(nums);
        System.out.println(i);
    }
}
